package com.dev.module_ui_composable.screen

import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.unit.Density

/**
 * ```
 * 换算公式：
 * px = density * dp
 * density = dpi / 160
 * px = dp * (dpi / 160)
 * px = sp * fontScale * density

 * 当前应用选择采用 3200px * 1440px 分辨率的屏幕适配，计算打印出各项参数如下：
 * densityDpi：560dpi
 * density: 3.5
 * widthPixels：1440
 * heightPixels：3007
 * widthDp约为：411.42856dp
 * heightDp约为：859.1429dp
 *
 * val displayMetrics = applicationContext.resources.displayMetrics
 * Log.i("TAG", "densityDpi: ${displayMetrics.densityDpi}")
 * Log.i("TAG", "density: ${displayMetrics.density}")
 * Log.i("TAG", "widthPixels: ${displayMetrics.widthPixels}")
 * Log.i("TAG", "heightPixels: ${displayMetrics.heightPixels}")
 * Log.i("TAG", "widthDp: ${displayMetrics.widthPixels / displayMetrics.density}")
 * Log.i("TAG", "heightDp: ${displayMetrics.heightPixels / displayMetrics.density}")
 ```
 * 以屏幕像素高度进行适配
 */
@Composable
fun AdaptationPixelHeight(
    content: @Composable () -> Unit
) {
    val fontScale = LocalDensity.current.fontScale
    val displayMetrics = LocalContext.current.resources.displayMetrics
    val heightPixels = displayMetrics.heightPixels

    CompositionLocalProvider(
        LocalDensity provides Density(
            density = heightPixels / 859f,
            fontScale = fontScale
        )
    ) {
        content()
    }
}

/**
 * 以屏幕像素宽度进行适配
 */
@Composable
fun AdaptationPixelWidth(
    content: @Composable () -> Unit
) {
    val fontScale = LocalDensity.current.fontScale
    val displayMetrics = LocalContext.current.resources.displayMetrics
    val widthPixels = displayMetrics.widthPixels

    CompositionLocalProvider(
        LocalDensity provides Density(
            density = widthPixels / 411.5f,
            fontScale = fontScale
        )
    ) {
        content()
    }
}